var http = require('http'),
    querystring = require('querystring'),
    wikimapiaAPIKey = '7B86479B-8F9EF6D2-3A7530C8-D6BBDD38-6DE385E3-D86991CD-6BB9FCDF-A6726FCB';

module.exports.execute = function (host, port, path, method, callback){

   //console.log("PROXY EXECUTION: HOST: " + host + ", PORT: " + port + ", PATH: " + path + ", METHOD: " + method);

         var options = {
                  host: host,
                  port: port,
                  path: path,
                  method: method
         };

         var response = "";

         http.request(options, function(res) {
            //console.log('STATUS: ' + res.statusCode);
            //console.log('HEADERS: ' + JSON.stringify(res.headers));
            res.setEncoding('utf8');
            
            res.on('data', function (json) {
               response += json;
            });
            
            res.on('end', function() {
               //console.log('BODY: ' + response);
               //console.log("------------------------------------------------------------------------------------------");
               /*if (res.statusCode == 200) {
                    var jsonObj = JSON.parse(response);
               }*/
               callback(response);
               return(this);
            });
    }).end();
}

module.exports.wikimapiaBboxSearch = function (bbox, category, count, page, callback) {

  var post_options = {
      host: 'api.wikimapia.org',
      port: '80',
      path: '/?function=box&key=' + wikimapiaAPIKey + '&bbox=' + bbox + '&disable=polygon&format=json' + (category ? '&category=' + category : '') + '&count=' + count + '&page=' + page,
      method: 'GET'
  };

  //console.log("------------------>>>>>>" + JSON.stringify(post_options));
  console.log("Llamando API box de Wikimapia.");
  console.log("--BBox: " + bbox);
  console.log("--Category: " + category);
  console.log("--Count: " + count);
  console.log("--Page: " + page);

  var response = "";

  var post_req = http.request(post_options, function(res) {
        //console.log('STATUS: ' + res.statusCode);
        //console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        
        res.on('data', function (json) {
           response += json;
        });
        
        res.on('end', function() {
           //console.log('BODY: ' + response);
           //console.log("------------------------------------------------------------------------------------------");
           var jsonObj = JSON.parse(response);
           callback(jsonObj);
           return(this);
        });
  });

  //post_req.write(post_data);
  post_req.end();
}

module.exports.wikimapiaGetObject = function (id, callback) {

  var post_options = {
      host: 'api.wikimapia.org',
      port: '80',
      path: '/?function=object&key=' + wikimapiaAPIKey + '&id=' + id + '&format=json',
      method: 'GET'
  };

  console.log("Llamando API Object de Wikimapia.");
  console.log("--ID: " + id);

  var response = "";

  var post_req = http.request(post_options, function(res) {
        //console.log('STATUS: ' + res.statusCode);
        //console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        
        res.on('data', function (json) {
           response += json;
        });
        
        res.on('end', function() {
           //console.log('BODY: ' + response);
           //console.log("------------------------------------------------------------------------------------------");
           var jsonObj = JSON.parse(response);
           callback(jsonObj);
           return(this);
        });
  });

  //post_req.write(post_data);
  post_req.end();
}
